﻿
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using DevComponents.DotNetBar;
using shopsoft.models;
using shopsoft.business.SystemConfig;
using shopsoft.Common.Security;
using System.Net;

namespace shopsoft.main
{
    public partial class LoginSystem : Office2007Form
    {
        #region Structure Method

        public LoginSystem()
        {
            InitializeComponent();
        }

        #endregion Structure Method

        #region Override

        /// <summary>
        /// 拖动工作区移动窗体
        /// </summary>
        /// <param name="m"></param>
        protected override void WndProc(ref Message m)
        {
            base.WndProc(ref m);
            if (m.Msg == 0x84)
            {
                switch (m.Result.ToInt32())
                {
                    case 1:
                        m.Result = new IntPtr(2);
                        break;
                }
            }
        }

        #endregion Override

        #region Event

        /// <summary>
        /// 窗体加载时间
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void LoginSystem_Load(object sender, EventArgs e)
        {
            this.txtUserNO.Focus();
        }

        /// <summary>
        /// 登录按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (ValidateInput())
            {
                var users = new shopsoft.models.Users()
                {
                    UserNO = txtUserNO.Text.Trim(),
                    PassWord = txtPassword.Text.Trim(),//Md5.MDString32(txtPassword.Text.Trim()),
                    IPAddress = ""//Dns.GetHostEntry(Dns.GetHostName()).AddressList[0].ToString()
                };

                if (new SCUserRoleAuthority().CheckUserPassword(users))
                {
                    Program.CurrentUser = new SCUserRoleAuthority().UsersLogin(users);
                    //Program.CurrentUser.LanguageType = (int)Language.LanguageType;
                    DialogResult = DialogResult.OK;
                }
                else
                {
                    MessageBox.Show("登录失败,请重新登陆!", "提示");
                    DialogResult = DialogResult.None;
                }
                //try
                //{
                    
                    
                //}
                //catch (Exception ex)
                //{
                //    MessageBox.Show(ex.Message, "提示");
                //    DialogResult = DialogResult.None;
                //}
            }
            else
            {
                DialogResult = DialogResult.None;
            }
        }

        /// <summary>
        /// 退出按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        #endregion

        #region Method

        /// <summary>
        /// 验证输入
        /// </summary>
        /// <returns></returns>
        private bool ValidateInput()
        {
            bool flag = false;
            errorProvider1.Clear();
            if (txtUserNO.Text.Trim() == string.Empty)
            {
                errorProvider1.SetError(txtUserNO, string.Format("请输入", "用户名"));
                txtUserNO.Focus();
            }
            else if (txtPassword.Text.Trim() == string.Empty)
            {
                errorProvider1.SetError(txtPassword, string.Format("请输入", "密码"));
                txtPassword.Focus();
            }
            else
            {
                flag = true;
            }
            return flag;
        }

        #endregion
    }
}
